<template>
  <section class="refund">
    <el-collapse-transition>
      <div class="course-search separate">
        <div class="course-search-inner">
          <el-row>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">交易流水号</div>
                <el-input
                  v-model.trim="listQuery.runningNumber"
                  style="width: 80%"
                  class="filter-item"
                  placeholder="请输入交易流水号"
                />
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">退款状态</div>
                <el-select v-model="listQuery.refundmentStatus" style="width: 80%" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </div>
            </el-col>

          </el-row>
        </div>
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>退款记录</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="sendRefundFun">发起退款</el-button>
        <el-button style="margin-left: 10px" type="primary" @click="backFun">返回</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        :tag="tableKey"
        :selection="false"
        :hover-arr="['balance']"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
      >
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          min-width="100"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleView(scope.row)">查看详情</el-button>

          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <!-- 选择入金账户 -->
    <el-dialog
      v-el-drag-dialog
      :destroy-on-close="true"
      :close-on-click-modal="false"
      width="800px"
      title="选择入金账户"
      center
      :visible.sync="innerVisible"
      append-to-body
    >
      <el-table v-loading="dlistLoading" style="height: 400px" :data="inAccount">
        <el-table-column property="name" label="账户名称" />
        <el-table-column property="accountNumber" :formatter="getNumber" label="账户号" />
        <el-table-column
          label="操作"
          width="50"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="chooseOne(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="dtotal > 0"
        :total="dtotal"
        :page.sync="dquery.page"
        :limit.sync="dquery.pageSize"
        @pagination="getAccFun()"
      />
    </el-dialog>
  </section>
</template>
<script>
import {
  refundList,
  getAcc,
  refund
} from './../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import CryptoJS from '@/utils/aesJiaJM'
import {
  mapGetters
} from 'vuex'
export default {
  name: 'Refund',
  components: {
    VTable,
    Pagination
  },
  directives: {
    elDragDialog
  },
  data() {
    return {
      innerVisible: false,
      dlistLoading: false,
      inAccount: [],
      dtotal: 0,
      dquery: {
        page: 1,
        pageSize: 10
      },
      listQuery: {
        page: 1,
        limit: 10,
        runningNumber: '',
        refundmentStatus: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      topInfo: {},
      backUrl: '',
      options: [
        {
          value: '',
          label: '全部'
        }, {
          value: '1',
          label: '发起退款'
        }, {
          value: '2',
          label: '发起退款失败'
        }, {
          value: '3',
          label: '退款受理中'
        },
				 {
          value: '4',
          label: '退款处理完成'
        }, {
          value: '5',
          label: '退款失败'
        }],
      tableKey: 'refundmentRecord',
      pageType: 'flb'
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  beforeRouteEnter(to, from, next) {
    console.log('from', from) // 可以拿到 from， 知道上一个路由是什么，从而进行判断
    next(vm => {
      if (from.fullPath.indexOf('refundInfo') == -1 && from.fullPath != '/') {
        sessionStorage.setItem('refundBack', from.fullPath)
      }
    })
  },
  created() {
    if (this.$route.query) {
      this.getList()
    } else {
      this.$message.error('页面参数错误')
      this.$router.back()
    }
  },
  methods: {
    getNumber(row, col) {
      return row['accountNumber'] ? CryptoJS.$aesJieM(row['accountNumber']) : ''
    },
    chooseOne(row) {
      this.insertRefund(row)
    },
    insertRefund(row) {
      const jm = CryptoJS.$aesJieM(row['accountNumber'])
      refund({ welfareId: this.$route.query.id, accountNumber: CryptoJS.$aesJiaM(jm) }).then(response => {
        if (response.data.code == '200') {
          this.getList()
        } else {
          this.$message.error(response.data.msg || '入金账户获取失败')
        }
      }).catch(error => {

      })
    },
    handleView(row) {
      this.$router.push({
        path: this.$filterPath('refundInfo'),
        query: { id: row.id }})
    },
    backFun() {
      this.$router.back()
    },
    sendRefundFun() {
      if (this.$route.query.isRefund === '0') {
        this.$message.error('账户没有可退余额，不能发起退款')
        return
      } else {
        this.getAccFun()
      }
    },
    getAccFun() {
      this.dlistLoading = true
      var query = {
        page: this.dquery.page,
        pageSize: this.dquery.pageSize,
        accountStatus: 'ZHZT01',
        accountType: 'INCOME'
      }
      return new Promise((resolve, reject) => {
        getAcc(query).then(response => {
          if (response.data.code == '200') {
            this.inAccount = response.data.data.rows || []
            this.dtotal = response.data.data.total
            this.dlistLoading = false
            if (this.dtotal == 0) {
              this.$message.error('当前未配置收款账户，请先配置收款账户。')
            } else if (this.dtotal == 1) {
              this.chooseOne(this.inAccount[0])
            } else {
              this.innerVisible = true
            }
          } else {
            this.$message.error(response.data.msg || '入金账户获取失败')
          }
        }).catch(error => {
          reject(error)
          this.dlistLoading = false
        })
      })
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.runningNumber = ''
      this.listQuery.refundmentStatus = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: this.listQuery.page,
        pageSize: this.listQuery.limit,
        welfareId: this.$route.query.id,
        runningNumber: this.listQuery.runningNumber ? this.listQuery.runningNumber : '',
        refundmentStatus: this.listQuery.refundmentStatus ? this.listQuery.refundmentStatus : ''
      }
      return new Promise((resolve, reject) => {
        refundList(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
	@import '@/styles/mixin.scss';
	$dark_gray: #bfced6;

	.refund {
		padding: 10px;

		.el-dialog .el-dialog__body {
			max-height: 73vh !important;
		}

		.line {
			text-align: center;
		}

		.el-card {
			margin-bottom: 10px;

			.current-position-name {
				font-size: 18px;
				font-weight: bold;
				margin-bottom: 10px;
			}
		}

		.course-search {
			background-color: #fff;
			padding: 0 10px;

			.svg-container {
				padding: 0 5px 0 10px;
				color: $dark_gray;
				vertical-align: middle;
				display: inline-block;

				&_login {
					font-size: 20px;
				}
			}

			.search-input {
				width: 60%;
				position: relative;

				&:after {
					content: "";
					position: absolute;
					left: -25px;
					right: 0;
					bottom: 8px;
					height: 1px;
					background: rgba(226, 227, 232, 1);
				}

				input {
					outline: none;
					background: transparent;
					border: none;
					outline: medium;
					padding: 0;

					&:focus {
						outline: none;
						background-color: transparent;
					}
				}
			}

			.course-search-btn {
				display: inline-block;
				float: right;

				.el-button.is-round {
					padding: 10px 30px;
					margin-right: 34px;
				}

				.course-search-more {
					font-size: 14px;
					font-weight: 500;
					@include font-color($mainColor);
					cursor: pointer;
					margin-left: 10px;
				}
			}

			&.merge {
				height: 50px;
				line-height: 50px;
			}

			&.separate {
				.course-search-inner {
					padding-top: 10px;

					// .el-input--medium .el-input__inner {
					// 	padding-left: 30px !important;
					// }
					.search-inner-item {
						.item-label {
							height: 30px;
							line-height: 30px;
							font-size: 14px;
							color: #a6a6a6;
							font-weight: 400;
						}

						.el-input {
							position: relative;

							.el-input__inner {
								border: none;
								padding: 0;
							}

							&:after {
								content: "";
								height: 1px;
								background-color: #e2e3e8;
								position: absolute;
								left: 0;
								right: 0;
								bottom: 0;
							}
						}
					}
				}

				.course-search-btn {
					text-align: center;
					display: block;
					float: none;
					height: 56px;
					line-height: 56px;
				}
			}
		}

		.main-buttons {
			display: flex;
			line-height: 36px;
			margin: 10px 0;

			.main-buttons-title {
				flex: 1;
				height: 36px;
				line-height: 36px;
				font-size: 16px;
				font-weight: bold;
				color: rgba(51, 51, 51, 1);

				.num {
					@include font-color($mainColor);
				}
			}
		}
	}
</style>
